﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace LightSwitchApplication.UserCode
{
    public class ImageHelper
    {
        public static byte[] GetImageByName(string fileName)
        {
            System.Reflection.AssemblyName thisAssemblyName = new System.Reflection.AssemblyName(typeof(ImageHelper).Assembly.FullName);
            Uri uri = new Uri(thisAssemblyName.Name + ";component/" + fileName, UriKind.Relative);

            if (uri == null)
            {
                return null;
            }
            //Load the resource from the URI
            var stream = System.Windows.Application.GetResourceStream(uri);

            return GetStreamAsByteArray(stream.Stream);
        }

        private static byte[] GetStreamAsByteArray(System.IO.Stream stream)
        {
            if (stream != null)
            {
                int streamLength = Convert.ToInt32(stream.Length);
                byte[] fileData = new byte[streamLength];
                stream.Read(fileData, 0, streamLength);
                stream.Close();
                return fileData;
            }
            else
            {
                return null;
            }
        }
    }
}
